package com.mohe.zhihui.action;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mohe.zhihui.model.Apply;
import com.mohe.zhihui.model.Message;
import com.mohe.zhihui.model.Project;
import com.mohe.zhihui.service.interfaces.IApply;
import com.mohe.zhihui.service.interfaces.IMessage;
import com.mohe.zhihui.service.interfaces.IProject;
import com.mohe.zhihui.utils.Json;
import com.mohe.zhihui.utils.JsonUtil;
import com.opensymphony.xwork2.ActionSupport;

public class ProjectReleaseAction extends ActionSupport {

	private static final long serialVersionUID = -9125625211127572692L;
	
//	ApplicationContext ac;
	
	private IProject projectServ;
	private IApply applyServ;
	private IMessage messageServ;
	
	private List<Project> prjList;
	private Apply apply = new Apply();
	private Long projectKey;
	private Message message = new Message();
	
	public IProject getProjectServ() {
		return projectServ;
	}

	public void setProjectServ(IProject projectServ) {
		this.projectServ = projectServ;
	}

	public IApply getApplyServ() {
		return applyServ;
	}

	public void setApplyServ(IApply applyServ) {
		this.applyServ = applyServ;
	}

	public IMessage getMessageServ() {
		return messageServ;
	}

	public void setMessageServ(IMessage messageServ) {
		this.messageServ = messageServ;
	}

	public List<Project> getPrjList() {
		return prjList;
	}

	public void setPrjList(List<Project> prjList) {
		this.prjList = prjList;
	}

	public Apply getApply() {
		return apply;
	}

	public void setApply(Apply apply) {
		this.apply = apply;
	}
	
	public Long getProjectKey() {
		return projectKey;
	}

	public void setProjectKey(Long projectKey) {
		this.projectKey = projectKey;
	}

//	public ProjectReleaseAction() {
//		super();
//		ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//		projectServ = (IProject) ac.getBean("IProject");
//		applyServ = (IApply) ac.getBean("IApply");
//	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public String getProjectList() {
		prjList = projectServ.getAllProject();
		return "project.jsp";
	}
	
	/**
	 * 保存申请人信息
	 */
	public void saveApply() {
		Json json = new Json();
		Project temp = projectServ.getProjectById(projectKey);
		apply.setProject(temp);
		boolean result = applyServ.saveApply(apply);
		if (result) {
			json.setMessage("添加成功");
		} else {
			json.setSuccess(false);
			json.setMessage("添加失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 保存留言信息
	 */
	public void saveMessage() {
		Json json = new Json();
		boolean result = messageServ.saveMessage(message);
		if (result) {
			json.setMessage("添加成功");
		} else {
			json.setSuccess(false);
			json.setMessage("添加失败");
		}
		JsonUtil.writeJson(json);
	}

}
